<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <div class="ke">
          <el-form-item label="可借次数" prop="number" label-width="80px">
            ：<b> {{ data.form.number }} </b>
          </el-form-item>
          <el-form-item label="可借金额" prop="amount" label-width="80px">
            ：<b> {{ data.form.amount }} </b>
          </el-form-item>
        </div>
        <div class="yi">
          <el-form-item label="已借次数" prop="borrowedNumber" label-width="80px">
            ： <b>{{ data.form.borrowedNumber }}</b>
          </el-form-item>
          <el-form-item label="已借金额" prop="borrowedAmount" label-width="80px">
            ：<b>{{ data.form.borrowedAmount }}</b>
          </el-form-item>
        </div>
      </el-row>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">申请预支</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">取消预支</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <el-divider />

    <div class="flex-container">
      <el-descriptions title="借支记录" direction="vertical" :column="4" border v-for="(item, index) in queryParams"
        :key="index">
        <el-descriptions-item v-if="item" label="借支记录号：">{{ item.borrowingLogId }}</el-descriptions-item>
        <el-descriptions-item v-if="item" label="借支金额：">{{ item.borrowingLogAmount }}</el-descriptions-item>
        <el-descriptions-item v-if="item" label="发起时间：" :span="2">{{ item.createTime }}</el-descriptions-item>
        <el-descriptions-item v-if="item" label="是否批准：">
          <el-tag size="small">{{ statusText(item.borrowingStatus) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item v-if="item" label="借支原由：">{{ item.reason }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 申请借支对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="borrowingRef" :model="queryParams" label-width="80px">
        <el-form-item label="预借金额" prop="borrowingLogAmount">
          <el-input v-model="form.borrowingLogAmount" placeholder="请输入预借金额" />
        </el-form-item>
        <el-form-item label="借支原由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入借支原由" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Borrowing">
import { listBorrowing, getBorrowing, delBorrowing, addBorrowing, updateBorrowing, getInfoByUserId } from "@/api/manage/borrowing";
import { associationList, listBorrowingLog, getBorrowingLog, delBorrowingLog, updateBorrowingLog, addBorrowingLog } from "@/api/manage/borrowingLog";

const { proxy } = getCurrentInstance();

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


function statusText(status) {
  if (status == 1)
    return "批准"
  if (status == 2)
    return "未批准"
  else
    return "审核中"
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    number: null,
    amount: null,
    userId: null,
    borrowedAmount: null,
    borrowedNumber: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    reason: null,
    borrowingLogAmount: null,
    borrowingStatus: null,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询借支权限列表 */
function getList() {
  loading.value = true;
  associationList(queryParams.value).then(response => {
    queryParams.value = response.rows;
    console.log(response.rows);
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    number: null,
    amount: null,
    userId: null,
    borrowedAmount: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    reason: null,
    borrowingLogAmount: null,
    borrowingStatus: null,
  };
  applyPre()
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "申请借支工资";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["borrowingRef"].validate(valid => {
    addBorrowingLog(form.value).then(response => {
      proxy.$modal.msgSuccess("申请成功");
      open.value = false;
      getList();
    });


  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除借支权限编号为"' + _userIds + '"的数据项？').then(function () {
    return delBorrowing(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 申请预支 */
function applyPre() {
  getInfoByUserId().then(response => {
    form.value = response.data;
  });
}

applyPre()
getList()

</script>

<style>
.ke b {
  color: #409EFF;
}

.yi b {
  color: #e41009;
}

.grid-container {
  display: grid;
  justify-items: end;
  /* 靠右对齐 */
}
</style>